package weibo.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionTools {
	private static final String url="jdbc:mysql://127.0.0.1:3306/weibo?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8";
	private static final String name="com.mysql.cj.jdbc.Driver";
	private static final String username="root";
	private static final String password="root";
	public static Connection conn=null;
	
	static{
		try {
			Class.forName(name);
//			System.out.println("数据库驱动访问成功");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("数据库驱动访问失败");
		}
	}
	/*
	 * 构造方法
	 * @param
	 * 
	 */
	public ConnectionTools(){
		try {
			conn=DriverManager.getConnection(url, username, password);
//			System.out.println("数据库连接存在");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("数据库连接出现错误");
		}
	}
	
	
	/*
	 * 获取连接
	 * @param
	 * @return conn
	 */
	public Connection getConn(){
		return conn;
	}
	/*
	 * 断开连接
	 * @param
	 * @return
	 */
	public void Close(){
		System.out.println("关闭数据库连接");
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO �Զ����ɵ� catch ��
			e.printStackTrace();
			System.out.println("连接关闭失败");
		}
	}
}
